package com.zhht.htzx.ui;

import android.accounts.Account;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.data.bean.MessageBean;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.ui.page.BaseActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.ItemNoticesListBinding;

import java.util.List;

/**
 * 公告列表
 */
public class NoticesListActivity extends BaseActivity {

    private StateHolder mStates;
    private AccountRequester mAccountRequester;
    private SimpleDataBindingAdapter<MessageBean.DataBean, ItemNoticesListBinding> mAdapter;

    @Override
    protected void initViewModel() {
        mStates = getActivityScopeViewModel(StateHolder.class);
        mAccountRequester = getActivityScopeViewModel(AccountRequester.class);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new SimpleDataBindingAdapter<MessageBean.DataBean, ItemNoticesListBinding>(this,
                R.layout.item_notices_list, new DiffUtil.ItemCallback<MessageBean.DataBean>() {
            @Override
            public boolean areItemsTheSame(@NonNull MessageBean.DataBean oldItem, @NonNull MessageBean.DataBean newItem) {
                return false;
            }

            @Override
            public boolean areContentsTheSame(@NonNull MessageBean.DataBean oldItem, @NonNull MessageBean.DataBean newItem) {
                return false;
            }
        }) {
            @Override
            protected void onBindItem(ItemNoticesListBinding binding, MessageBean.DataBean item, RecyclerView.ViewHolder holder) {
                binding.setVm(item);
                binding.tvItemNoticesContent.setText(Html.fromHtml(item.getContent()));
                setOnItemClickListener(new OnItemClickListener<MessageBean.DataBean>() {
                    @Override
                    public void onItemClick(int viewId, MessageBean.DataBean item, int position) {
                        startActivity(new Intent()
                                .putExtra("data", item)
                                .setClass(NoticesListActivity.this, NoticesDetailActivity.class));
                    }
                });
            }
        };
        return new DataBindingConfig(R.layout.activity_notices_list, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mStates.titleBar.set("公告");
        getLifecycle().addObserver(mAccountRequester);
        mAccountRequester.getNotices();

        mAccountRequester.getMessageListResult().observe(this, new Observer<ResponseStatus<List<MessageBean.DataBean>>>() {
            @Override
            public void onChanged(ResponseStatus<List<MessageBean.DataBean>> listResponseStatus) {
                if (listResponseStatus.getResponseCode() == 0) {

                    mStates.noticesList.set(listResponseStatus.getData());

                }
            }
        });
    }
}
